home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / mesa / mesa-aux / book.aux / stroke.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  5KB  |  222 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. /*
  38.  *  stroke.c 
  39.  *  This program demonstrates some characters of a 
  40.  *  stroke (vector) font.  The characters are represented
  41.  *  by display lists, which are given numbers which 
  42.  *  correspond to the ASCII values of the characters.
  43.  *  Use of glCallLists() is demonstrated.
  44.  */
  45. #include <GL/gl.h>
  46. #include <GL/glu.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <GL/glaux.h>
  50.  
  51. #define PT 1
  52. #define STROKE 2
  53. #define END 3
  54.  
  55. typedef struct charpoint {
  56.   GLfloat x, y;
  57.   int type;
  58. } CP;
  59.  
  60. CP Adata[] =
  61. {
  62.   {0, 0, PT},
  63.   {0, 9, PT},
  64.   {1, 10, PT},
  65.   {4, 10, PT},
  66.   {5, 9, PT},
  67.   {5, 0, STROKE},
  68.   {0, 5, PT},
  69.   {5, 5, END}
  70. };
  71.  
  72. CP Edata[] =
  73. {
  74.   {5, 0, PT},
  75.   {0, 0, PT},
  76.   {0, 10, PT},
  77.   {5, 10, STROKE},
  78.   {0, 5, PT},
  79.   {4, 5, END}
  80. };
  81.  
  82. CP Pdata[] =
  83. {
  84.   {0, 0, PT},
  85.   {0, 10, PT},
  86.   {4, 10, PT},
  87.   {5, 9, PT},
  88.   {5, 6, PT},
  89.   {4, 5, PT},
  90.   {0, 5, END}
  91. };
  92.  
  93. CP Rdata[] =
  94. {
  95.   {0, 0, PT},
  96.   {0, 10, PT},
  97.   {4, 10, PT},
  98.   {5, 9, PT},
  99.   {5, 6, PT},
  100.   {4, 5, PT},
  101.   {0, 5, STROKE},
  102.   {3, 5, PT},
  103.   {5, 0, END}
  104. };
  105.  
  106. CP Sdata[] =
  107. {
  108.   {0, 1, PT},
  109.   {1, 0, PT},
  110.   {4, 0, PT},
  111.   {5, 1, PT},
  112.   {5, 4, PT},
  113.   {4, 5, PT},
  114.   {1, 5, PT},
  115.   {0, 6, PT},
  116.   {0, 9, PT},
  117.   {1, 10, PT},
  118.   {4, 10, PT},
  119.   {5, 9, END}
  120. };
  121.  
  122. /*
  123.  * drawLetter() interprets the instructions from the array
  124.  * *  for that letter and renders the letter with line segments.
  125.  */
  126. void drawLetter(CP * l)
  127. {
  128.   glBegin(GL_LINE_STRIP);
  129.   while (1) {
  130.     switch (l->type) {
  131.       case PT:
  132.     glVertex2fv(&l->x);
  133.     break;
  134.       case STROKE:
  135.     glVertex2fv(&l->x);
  136.     glEnd();
  137.     glBegin(GL_LINE_STRIP);
  138.     break;
  139.       case END:
  140.     glVertex2fv(&l->x);
  141.     glEnd();
  142.     glTranslatef(8.0, 0.0, 0.0);
  143.     return;
  144.     }
  145.     l++;
  146.   }
  147. }
  148.  
  149. /*
  150.  * Create a display list for each of 6 characters       
  151.  */
  152. void myinit(void)
  153. {
  154.   GLuint base;
  155.  
  156.   glShadeModel(GL_FLAT);
  157.  
  158.   base = glGenLists(128);
  159.   glListBase(base);
  160.   glNewList(base + 'A', GL_COMPILE);
  161.   drawLetter(Adata);
  162.   glEndList();
  163.   glNewList(base + 'E', GL_COMPILE);
  164.   drawLetter(Edata);
  165.   glEndList();
  166.   glNewList(base + 'P', GL_COMPILE);
  167.   drawLetter(Pdata);
  168.   glEndList();
  169.   glNewList(base + 'R', GL_COMPILE);
  170.   drawLetter(Rdata);
  171.   glEndList();
  172.   glNewList(base + 'S', GL_COMPILE);
  173.   drawLetter(Sdata);
  174.   glEndList();
  175.   glNewList(base + ' ', GL_COMPILE);
  176.   glTranslatef(8.0, 0.0, 0.0);
  177.   glEndList();
  178. }
  179.  
  180. char *test1 = "A SPARE SERAPE APPEARS AS";
  181. char *test2 = "APES PREPARE RARE PEPPERS";
  182.  
  183. void printStrokedString(char *s)
  184. {
  185.   GLsizei len = strlen(s);
  186.  
  187.   glCallLists(len, GL_BYTE, (GLbyte *) s);
  188. }
  189.  
  190. void display(void)
  191. {
  192.   glClear(GL_COLOR_BUFFER_BIT);
  193.   glColor3f(1.0, 1.0, 1.0);
  194.   glPushMatrix();
  195.   glScalef(2.0, 2.0, 2.0);
  196.   glTranslatef(10.0, 30.0, 0.0);
  197.   printStrokedString(test1);
  198.   glPopMatrix();
  199.   glPushMatrix();
  200.   glScalef(2.0, 2.0, 2.0);
  201.   glTranslatef(10.0, 13.0, 0.0);
  202.   printStrokedString(test2);
  203.   glPopMatrix();
  204.   glFlush();
  205. }
  206.  
  207. /*
  208.  * Main Loop
  209.  * *  Open window with initial window size, title bar, 
  210.  * *  RGBA display mode, and handle input events.
  211.  */
  212. int main(int argc, char **argv)
  213. {
  214.   auxInitDisplayMode(AUX_SINGLE | AUX_RGB);
  215.   auxInitPosition(0, 0, 440, 120);
  216.   if (!auxInitWindow(argv[0]))
  217.     auxQuit();
  218.   myinit();
  219.   auxMainLoop(display);
  220.   return 0;
  221. }
  222.